
var tableName ="";
var fieldName ="";
var fieldsName="";
var backField ="";

//DWR多行保存
//form,表名,普通控件名称,多行控件名称,数据回显
function doAjaxInsertSql(form,table,field,fields){
	var fieldArray  = field.split(",");
	var fieldsArray = fields.split(",");
	var sqlString 	= "";
	var flag 		= "-1";
	fieldsArrayL = form[fieldsArray[0]];
	if(fields!=""&&fieldsArrayL.length){
		for(x=0;x<fieldsArrayL.length;x++){
						
			sql = "INSERT INTO "+table+"("+field
			if(field!=""&fields!="")sql+=",";
			sql+=fields+") VALUES(";
			
				if(field!="")
				for(var i=0;i<fieldArray.length;i++){
				if(i!=0)sql+=","
				
				sql+="'"+form[fieldArray[i]].value+"'";
				}
				if(field!=""&fields!="")sql+=",";
				
				if(fields!="")
				for(var i=0;i<fieldsArray.length;i++){
				if(i!=0)sql+=","
				
				sql+="'"+form[fieldsArray[i]][x].value+"'";
				}
				 
			sql+=")";	
			
			if(sqlString.length!=0)sqlString+="<><>";
			sqlString +=sql;
			
		}
			
	
	}else{
		sql = "INSERT INTO "+table+"("+field
		if(field!=""&fields!="")sql+=",";
		sql+=fields+") VALUES(";
		
			if(field!="")
			for(var i=0;i<fieldArray.length;i++){
			if(i!=0)sql+=","
			
			sql+="'"+form[fieldArray[i]].value+"'";
			}
		
			if(field!=""&fields!="")sql+=",";
			
			if(fields!="")
			for(var i=0;i<fieldsArray.length;i++){
			if(i!=0)sql+=","
			
			sql+="'"+form[fieldsArray[i]].value+"'";
			}
			 
			sql+=")";	
			sqlString =sql;
	}
	
	return sqlString;
	
}

function clearForm(form,fieldName,fieldsName,backFieldName){
	var fieldArray 		 = fieldName.split(",");
	var fieldsArray 	 = fieldsName.split(",");
 
//普通表单元素清除
		if(fieldName!="")
			for(var i=0;i<fieldArray.length;i++){
				if(backData(backFieldName,fieldArray[i]))continue;
				if(form[fieldArray[i]].type&&form[fieldArray[i]].tagName=="SELECT"){
				clearOptions(form[fieldArray[i]]);
				}
			form[fieldArray[i]].value="";
			}

//添加行表单元素清除
	fieldsArrayL = form[fieldsArray[0]];
	
	if(fieldsName!=""&&fieldsArrayL.length){
	 
		for(var i=0;i<fieldsArray.length;i++){
					if(backData(backFieldName,fieldsArray[i]))continue;
					
					if(form[fieldsArray[i]][0].type&&form[fieldsArray[i]][0].tagName=="SELECT"){
					clearOptions(form[fieldsArray[i]][0]);
					}else{
					form[fieldsArray[i]][0].value="";
					}
				}
				
		var llen = fieldsArrayL.length;	
		for(x=1;x<llen;x++){
		 var tr = fieldsArrayL[1].parentNode.parentNode;
		 var table = tr.parentNode;
		 table.removeChild(tr);
		}
		
	}else{
				if(fieldsName!="")
				for(var i=0;i<fieldsArray.length;i++){
					if(backData(backFieldName,fieldsArray[i]))continue;
					
					if(form[fieldsArray[i]].type&&form[fieldsArray[i]].tagName=="SELECT"){
					clearOptions(form[fieldsArray[i]]);
					}else{
					form[fieldsArray[i]].value="";
					}
				}
	}

}
////保留数据
function backData(backField,str){
	var backArray = backField.split(",")
	for(var h=0;h<backArray.length;h++){
		 if(backArray[h]==str){
		 return true;
		 }else{
		 }
	}
	return false;
}

///把form内容生成存储过程语句 

function callSql(stringSql,callName,callField,callFields){

		form = document.forms[0];
	
		deleteId = form.deteleId;

		/*
	1.	b不为空 	b多行 a为空
	2.	b不为空 	b多行 a不为空 
		
	3. 	b不为空 	b单行 a为空 
	4.	b不为空 	b单行 a不为空 
	 		
	5. 	b为空 a为空 
	6. 	b为空 a不为空 
	 */
			if(callFields!=""){

			var callArrays = callFields.split(",");
			var someone = form[callArrays[0]];
			
				if(someone&&someone.length){
				bsString = "";
					for(var i=0;i<someone.length;i++){
					if(someone[i].readOnly == true){
						continue;
					}
					var sql ="";
					
						if(bsString.length!=0)bsString+="<><>";
						
							var sql  = "call "+callName + "(";
							for(var k=0;k<callArrays.length;k++){
								if(k!=0)sql+=",";
								sql += "'"+form[callArrays[k]][i].value+"'";
							}
							
							
							
							if(callField!=""){
							sql+=",";
							
								var callArray = callField.split(",");
								for(var k=0;k<callArray.length;k++){
									if(k!=0)sql+=",";
									sql += "'"+form[callArray[k]].value+"'";
								}
							}
							
							sql += ")";
						
						bsString+=sql;
					}
					if(stringSql.length!=0)stringSql+="<><>";
					stringSql+=bsString;
				}else{ 
						
							var sql  = "call "+callName + "(";
							
							for(var k=0;k<callArrays.length;k++){
								if(k!=0)sql+=",";
								sql += "'"+form[callArrays[k]].value+"'";
							}
							if(callField!=""){
							sql+=",";
								var callArray = callField.split(",");
								for(var k=0;k<callArray.length;k++){
									if(k!=0)sql+=",";
									sql += "'"+form[callArray[k]].value+"'";
								}
							}
							sql += ")";
						if(stringSql.length!=0)stringSql+="<><>";
						stringSql+=sql;
			}
		} else{
			if(stringSql.length!=0)stringSql+="<><>";
							
								var sql  = "call "+callName + "(";
								
								if(callField!=""){
									var callArray = callField.split(",");
									for(var k=0;k<callArray.length;k++){
										if(k!=0)sql+=",";
										sql += "'"+form[callArray[k]].value+"'";
									}
								}
								
								sql += ")";
							
							stringSql+=sql;
		}
		
		
	return stringSql;

}
////////////////////////////
// ----  baseUrl   --------------↓
var ctxpath = window.location.pathname;
var a = window.location.pathname.split('/');
var baseUrl = '/' + a[1];
// ----   $import  --------------↓
function $import(ctx,path) {
	document.write("<script src=\"" + ctx + "/" + path + "\"></script>");
}
//$import(baseUrl,'parts/framejs/ajax/jsonPaging.js');

/////////
//DWREngine.setAsync(false); 

//页面下拉框类型 json格式赋值---------↓
function initSelect(element,optionsInfo){
	clearOptions(element);
	for (var o in optionsInfo) { 
			       element.appendChild(createOption(o, optionsInfo[o]));
			       element.style.width="100%";
			       element.options[0].value="";
			       element.options[0].text="请选择";
			       }
}
function initSelectL(element,optionsInfo){
	clearOptions(element);
	for (var o in optionsInfo) { 
			       element.appendChild(createOption(o, optionsInfo[o]));
			       element.options[0].value="";
			       element.options[0].text="请选择";
			       }
}
//----------------------↑



//打开窗口
function openWin(url,width,height,flag,top,left) {
	var str = 'status = no,width = ' + width + ',innerWidth = ' + width;
	str		= str + ',height = ' + height + ',innerHeight = ' + height;
	if(flag)
		str = str + ',resizable = yes';
	else
		str = str + ',resizable = no';
	if(window.screen) {
		if(top != null)
			str = str + ',top = ' + top + ',screenY = ' + top;
		else {
			var ah = screen.availHeight - 30;
			var yc = (ah - height) / 2;
			str = str + ',top = ' + yc + ',screenY = ' + yc;			
		}
		if(left != null)
			str = str + ',left = ' + left + ',screenX = ' + left;
		else {
			var aw = screen.availWidth - 10;
			var xc = (aw - width) / 2;
			
			str = str + ',left = ' + xc + ',screenX = ' + xc;
		}
	}
	var winname = 'win' + Math.random().toString(10).substr(2);
	return window.open(url,winname,str);
}

function getLocation(name) {
	var prefix = name + "=";
	var search = window.location.search;
	var start =  search.indexOf(prefix);

	if (start==-1) {
		return null;
	}
	var end = search.indexOf("&", start+prefix.length); 
	if (end==-1) {
		end=search.length;
	}
	var value=search.substring(start+prefix.length, end);
	return unescape(value);
}

//EXT备注空间
var win;
var form;
var text;
function getInsertForm(){
	return new Ext.FormPanel({
		labelWidth: 40,
        frame:true,
        bodyStyle:'padding:5px 5px 0',
        width: 100,
        defaults: {width: 230},
        defaultType: 'textarea',
        items: [{
                fieldLabel: '内容',
                name: 'DivId',
                id: 'DivId',
                width: 180,
                height: 200
            }
        ]
    });
}
function initGrid(){
	if(!form){
	    form=getInsertForm();
	}
	if(!win){
		win = new Ext.Window({
		    el:'add-window',
		    layout:'fit',
		    width:280,
		    height:290,
		    closeAction:'hide',
		    buttons: [{
		        text: '确定',
		        handler: function(){
		        	text.value = document.all["DivId"].value
		            win.hide();
		        }
		    },{
		        text: '关闭',
		        handler: function(){
		            win.hide();
		        }
		    }]
		});
	}
};
function doMoveDiv(obj,val){
	if(document.all["MoveDiv"].innerHTML.indexOf("content")!=-1){
		document.all["MoveDiv"].innerHTML = document.all["MoveDiv"].innerHTML.replace('content',val)
	}
	text = obj;
	var items=new Ext.util.MixedCollection();
   	items.add("form",form);
   	win.items=items;
	win.show(obj);
   	form.getForm().reset();
   	document.all["DivId"].value = obj.value
}
//////
function doEnters(){
	if(event.keyCode==13&&document.all["my_div"].style.display==""&&stateDiv!=null&&stateDiv.outerText!=undefined){
		stateVal.value=stateDiv.outerText;
		var begin = stateDiv.outerHTML.indexOf("value")+6;
		var end =stateDiv.outerHTML.length-6;
		stateVal.parentNode.lastChild.value=stateDiv.outerHTML.substring(begin,end)
		document.all["my_div"].style.display="none";
	}
}
function doEnter(){
		stateVal.value=stateDiv.outerText;
		var begin = stateDiv.outerHTML.indexOf("value")+6;
		var end =stateDiv.outerHTML.length-6;
		stateVal.parentNode.lastChild.value=stateDiv.outerHTML.substring(begin,end)
}
//name为要赋值的控件的name属性
function doDate(name){
	var date=new Date();
	var month=date.getMonth()+1;
	var day=date.getDate();
	if(month<10){
		month="0"+month;
	}
	if(day<10){
		day="0"+day;
	}
	document.all[name].value=date.getYear()+"-"+month+"-"+day;
}
